package rabbitmq

import (
	"github.com/streadway/amqp"
)

// Exchange Types
const (
	EXCHANGE_TYPE_DIRECT  = "direct"  // 直连交换机
	EXCHANGE_TYPE_FANOUT  = "fanout"  // 扇型交换机
	EXCHANGE_TYPE_TOPIC   = "topic"   // 主题交换机
	EXCHANGE_TYPE_HEADERS = "headers" // 头交换机
)

type ExchangeOpt func(*Exchange)

type Exchange struct {
	Name       string     // 交换机名称
	Type       string     // 交换机类型
	Durable    bool       // 交换机是否持久化
	AutoDelete bool       // 交换机不再使用时，是否自动删除
	Args       amqp.Table // 依赖代理本身的一些功能
}

func NewExchange(opts ...ExchangeOpt) *Exchange {
	e := &Exchange{
		Durable: true,
	}
	for _, o := range opts {
		o(e)
	}

	return e
}

func ExchangeName(name string) ExchangeOpt {
	return func(e *Exchange) {
		e.Name = name
	}
}

func ExchangeType(t string) ExchangeOpt {
	return func(e *Exchange) {
		e.Type = t
	}
}

func ExchangeDurable(durable bool) ExchangeOpt {
	return func(e *Exchange) {
		e.Durable = durable
	}
}

func ExchangeAutoDelete(autoDelete bool) ExchangeOpt {
	return func(e *Exchange) {
		e.AutoDelete = autoDelete
	}
}

func ExchangeArgs(args map[string]interface{}) ExchangeOpt {
	return func(e *Exchange) {
		e.Args = args
	}
}

func checkExchangeType(t string) bool {
	if t == EXCHANGE_TYPE_DIRECT ||
		t == EXCHANGE_TYPE_FANOUT ||
		t == EXCHANGE_TYPE_TOPIC ||
		t == EXCHANGE_TYPE_HEADERS {
		return true
	}

	return false
}
